#!/data/Software/mydan/perl/bin/perl -I/data/Software/mydan/CI/lib -I/data/Software/mydan/CI/private/lib
use strict;
use warnings;
use Code;
use Logs;
use LWP::UserAgent;

$| ++;

=head1 SYNOPSIS

    db => $mysql,
    logs => 日志对象

    uuid => version的uuid

#只有使用uuid来构建的时候才允许send

=cut

return sub
{
    my %param = @_;

    my ( $db, $logs, $uuid, $projectid, $version, $callback )
        = @param{qw( db logs uuid projectid version callback )};

    print "=" x 75, "\ncallback($callback)...\n";

    $logs = Logs->new( 'code.build.send.callback' ) unless $logs;

    my $name = eval{ Code->new( "projectname" )->run( id => $projectid ); };
    $logs->die( "get projectname fail:$@" ) if $@;
    print "projectname: $name\n";
    $logs->die( "projectname format error" ) unless $name && $name =~ /^[a-zA-Z0-9][a-zA-Z0-9\._\-]+$/;

    my $ua = LWP::UserAgent->new();
    $ua->agent('Mozilla/9 [en] (Centos; Linux)');
    $ua->timeout( 10 );
    $ua->default_header ( 'Cache-control' => 'no-cache', 'Pragma' => 'no-cache' );

    my $res = $ua->post( $callback, projectid => $projectid, version => $version, projectname => $name );
    $logs->die( "callback return not 200" ) if $res->code() ne 200;
}
